package xx.pro.controller;

import io.minio.messages.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import xx.pro.util.MinioUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/test")
@RestController
public class TestCon {
    @Resource
    MinioUtil minioUtil;

    /**
     * 判断桶位是否存在
     * @param bucketName
     * @return
     */
    @GetMapping("/bucketExists")
    public Boolean bucketExists(String bucketName) {
        return minioUtil.bucketExists(bucketName);
    }

    /**
     * 创建桶位
     * @param bucketName
     * @return
     */
    @GetMapping("/makeBucket")
    public Boolean makeBucket(String bucketName) {
        return minioUtil.makeBucket(bucketName);
    }

    /**
     * 获取所有的桶位
     * @return
     */
    @GetMapping("/getAllBuckets")
    public List<String> getAllBuckets() {
        return minioUtil.getAllBuckets();
    }

    /**
     * 根据桶位名称获取桶位信息
     * @param bucketName
     * @return
     */
    @GetMapping("/getBucketInfo")
    public Bucket getBucketInfo(String bucketName) {
        return minioUtil.getBucket(bucketName);
    }

    /**
     * 删除桶位
     * @param bucketName
     * @return
     */
    @GetMapping("/delBucket")
    public Boolean delBucket(String bucketName) {
        return minioUtil.removeBucket(bucketName);
    }

    /**
     * 上传文件
     * @param multipartFile
     * @return
     */
    @PostMapping("/uploadFile")
    public String uploadFile(MultipartFile multipartFile, String bucketName) {
        return minioUtil.upload(multipartFile, bucketName);
    }

    /**
     * 预览文件
     * @param fileName
     * @param bucketName
     * @return
     */
    @PostMapping("/preview")
    public String preview(String fileName, String bucketName) {
        return minioUtil.preview(fileName, bucketName);
    }

    /**
     * 下载文件
     * @param fileName
     * @param bucketName
     * @param response
     */
    @GetMapping("/download")
    public void download(String fileName, String bucketName, HttpServletResponse response) {
        minioUtil.download(fileName, bucketName, response);
    }
}

